
import { showModal } from '@/utils/message';
import { MessageBox } from 'element-ui';
import store from '@/store';
import { requestImg } from '@/api/qiniu';

export function showValidateMsg(data, options = {}, successCallBack, failCallback) {
  if (!data.length) return;
  let message = '';
  data.forEach(i => {
    message += `<p style="font-size:16px;color: #333;font-weight: 600;word-break: break-all;">${i}</p>`;
  });
  showModal((message), '', {
    confirmButtonText: '确定',
    showCancelButton: options.showCancelButton || false,
    showClose: false,
    type: 'warning',
    center: true,
    iconClass: 'charge-icon',
    customClass: options.customClass || 'error-message',
    dangerouslyUseHTMLString: true
  }, options.catchReject).then(() => {
    successCallBack && successCallBack();
  }).catch(() => {
    failCallback && failCallback();
  });
}

// 表单中存在多个富文本框时让每个富文本框z-index大于后一个，避免弹窗内容被覆盖
export function handleRicheditorIndex() {
  const richeditorList = document.querySelectorAll('.richeditor');
  if (richeditorList.length && richeditorList.length > 1) {
    richeditorList.forEach((it, idx) => {
      it.style.zIndex = richeditorList.length - idx;
    });
  }
}

export function handleCreateAgain(resolve, reject, h) {
  const showMobilePage = store.state.app.showMobilePage;
  const handelOk = () => {
    MessageBox.close();
    resolve();
  };
  const rejectFun = () => {
    MessageBox.close();
    reject();
  };
  MessageBox.confirm('', '', {
    message: h('p', null, [
      h('div', null, '该审批表单非最新版本'),
      h('div', null, '请确认是否引用旧版表单发起？'),
      h('div', null, [
        h('el-button', { on: { click: handelOk }, props: { type: 'primary', size: 'small' }, style: 'margin-top: 20px;' }, '使用新表单重新发起'),
        h('el-button', { on: { click: rejectFun }, props: { size: 'small' }, style: 'margin-top: 20px;', class: 'cancel-btn-style' }, '引用旧表单发起')
      ])
    ]),
    showCancelButton: false,
    showConfirmButton: false,
    type: 'warning',
    customClass: `del-message ${showMobilePage ? ' mobile-message' : ''}`,
    iconClass: 'charge-icon',
    center: true
  });
}

/**
 *
 * @param {*} type 0微博 1微信
 */
export function getImgList(imgList, type, isVideo) {
  /**
    {
      name: string,
      time: number,
      url: string
    }
   */
  return new Promise(async(resolve, reject) => {
    const result = [];
    if (imgList && imgList.length > 0) {
      const request = [];
      imgList.forEach(async(it, index) => {
        request.push(requestImg({ url: it, type }));
      });

      try {
        const resArr = await Promise.all(request);
        if (resArr && resArr.length) {
          resArr.forEach((res, index) => {
            result.push({
              url: res.data,
              name: isVideo ? `视频${index + 1}.mp4` : `图片${index + 1}.png`,
              time: new Date().getTime()
            });
          });
        }

        resolve(result);
      } catch (error) {
        console.log(error);
        resolve([]);
      }
    }
    resolve(result);
  });
}
